/*
 * DirEntry.cpp
 *
 *  Created on: Mar 25, 2010
 *      Author: Lukasz Spintzyk
 */

#include "DirEntry.h"


DirEntry::DirEntry()
{
}

DirEntry::DirEntry(const std::string name, uint32_t inodeNum, uint32_t fileType)
	:d_name(name)
	,d_inodeNum(inodeNum)
	,d_fileType(fileType)
{

}

DirEntry::~DirEntry()
{
}

uint32_t DirEntry::size()
{
	uint32_t size = 0;
	size += sizeof(d_inodeNum);

	size += d_name.size();
	//std::cout << " size " << d_name.size();
	size += sizeof(byte);

	size += sizeof(d_fileType);
	size += sizeof(d_recordLen);

	byte min_padding = 0;
	while((size+min_padding)%4)
	{
		min_padding++;
	}

	return size+min_padding;
}

void DirEntry::read(const byte* d_buffer)
{
	uint32_t* p = (uint32_t*) d_buffer;
	d_buffer+= sizeof(uint32_t);

	uint16_t* p16 = (uint16_t*) d_buffer;
	d_inodeNum = p[0];
	d_recordLen = p16[0];
	d_buffer+=sizeof(uint16_t);
	d_nameLen = d_buffer[0];
	d_fileType = d_buffer[1];
	d_buffer+=2;
	d_name = std::string(d_buffer, d_buffer+d_nameLen);

	//d_name.assign((const char*)d_blocks+2, (size_t)d_nameLen);
}

void DirEntry::write(byte* buffer)
{
	uint32_t* p = (uint32_t*) buffer;
	buffer+= sizeof(uint32_t);
	uint16_t* p16 = (uint16_t*) buffer;
	p[0] = d_inodeNum;
	p16[0] = d_recordLen;
	buffer+=sizeof(uint16_t);
	d_nameLen = strlen(d_name.c_str());
	assert(d_nameLen == d_name.size());
	buffer[0] = d_nameLen;
	buffer[1] = d_fileType;
	buffer+=2;
	memcpy(buffer, d_name.c_str(), d_nameLen);
}
